package com.atguigu.ds.impl;

import com.atguigu.ds.List;

import java.util.function.Consumer;

public class LinkedList<E> implements List<E> {

    private static class Node<E>{
        E item ;
        Node<E> next ;
        Node(E item , Node<E> next){
            this.item = item ;
            this.next = next ;
        }
    }

    //记录元素的实际个数
    private int size = 0 ;

    //记录头结点的位置(只有记录了头结点的位置才能获取到链表中的每一个节点值)
    private Node<E> header ;


    @Override
    public int size() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size==0;
    }

    @Override
    public void add(E element) {
        add(size,element);
    }

    @Override
    public void add(int index, E element) {
        if(index<0 || index>size){
            throw new IndexOutOfBoundsException(index);
        }
        //在第一个位置添加元素
        if(index==0){
            Node<E> newNode = new Node<>(element,header);
            header = newNode ;
            size++;
        }else {
            Node<E> pre = getNode(index - 1);
            Node<E> curr = getNode(index);

            Node<E> newNode = new Node<>(element, curr);
            pre.next = newNode;
            size++;
        }
    }

    public Node<E> getNode(int index) {
        if(index<0 || index>size){
            throw new IndexOutOfBoundsException(index);
        }
        int i = 0 ;
        Node<E> curr = header ;
        while(i<index){
            curr = curr.next ;
            i++;
        }
        return curr;
    }

    @Override
    public E get(int index) {
        return getNode(index).item;
    }

    @Override
    public E remove(int index) {
        if(size<=0){
            return null ;
        }
        if(index<0 || index>=size){
            throw new IndexOutOfBoundsException(index);
        }
        Node<E> curr = null ;
        if(index==0){
            curr = header ;
            header = header.next ;
            size--;
        }else if(index==size-1){
            curr = getNode(index);
            Node<E> pre = getNode(index-1);
            pre.next = null ;
            size--;
        }else{
            curr = getNode(index) ;
            Node<E> pre = getNode(index-1);
            pre.next = pre.next.next ;
            size--;
        }
        return curr.item;
    }

    @Override
    public E set(int index, E element) {
        if(index<0 || index>=size){
            throw new IndexOutOfBoundsException(index);
        }
        Node<E> curr = getNode(index);
        E oldValue = curr.item ;
        curr.item = element ;
        return oldValue;
    }

    @Override
    public void forEach(Consumer<E> consumer) {
        Node<E> curr = header ;
        while(curr!=null){
            consumer.accept(curr.item);
            curr = curr.next;
        }
    }

    @Override
    public String toString(){
        StringBuilder stringBuilder = new StringBuilder("[");

        Node<E> curr = header ;
        while(curr!=null){
            stringBuilder.append(curr.item).append(",");
            curr = curr.next;
        }
        stringBuilder.deleteCharAt(stringBuilder.length()-1);

        return stringBuilder.append("]").toString() ;
    }
}
